package com.willter.photoapp.ui;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.willter.photoapp.R;
import com.willter.photoapp.R.id;
import com.willter.photoapp.R.layout;
import com.willter.photoapp.R.menu;
import com.willter.photoapp.adapter.ImageAdapter;
import com.willter.photoapp.utils.FileList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class MainActivity extends Activity {

	private List<Map<String, List<File>>> dirs;
	private GridView gridView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		gridView = (GridView) findViewById(R.id.gridView);
		dirs = FileList.findDirectory("/mnt/sdcard/DCIM", dirs);
		ImageAdapter adapter = new ImageAdapter(this, dirs);
		gridView.setAdapter(adapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
				Intent intent = new Intent(MainActivity.this, ImageViewActivity.class);
				String dir = dirs.get(position).entrySet().iterator().next().getKey();
				intent.putExtra("dir", dir);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
